package com.sghd.fight.module.game.service.skill.effect;

import com.sghd.fight.module.game.config.SkillConfig;
import com.sghd.fight.module.game.core.Chess;
import com.sghd.fight.module.game.core.ChessBoard;
import com.sghd.fight.module.game.core.Grid;
import com.sghd.fight.module.game.core.model.Unit;
import com.sghd.fight.module.game.service.room.IRoom;
import com.sghd.fight.module.game.service.skill.Context;
import com.sghd.pojo.fight.game.*;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author frank
 * @date 2018-12-05 9:37
 * @desc 能量转换效果
 **/
@Component
public class ConvertEffect implements Effect {

    @Override
    public void execute(Context context, IRoom room, Unit owner, Unit target, Map<String, Object> ctx, SkillReport skillReport, TargetReport targetReport, SkillConfig config) {
        ChessBoard chessBoard = room.getChessBoard();
        ChessType sourceType = ChessType.valueOf(ctx.get(SOURCE).toString());
        ChessType targetType = ChessType.valueOf(ctx.get(TARGET).toString());
        int attack = 0; //攻力
        for (Grid grid : chessBoard.getGrids()) {
            if (grid.getType() == sourceType) {
                attack++;
                Chess sour = grid.getChess();
                grid.fill(new Chess(sour.getState(), targetType));
            }
        }
        if (attack == 0) { //未完成转换
            context.addValue(target, UnitValue.HP, 0);
            targetReport.addValue(ValueItem.valueOf(UnitValue.HP, 0));
            return;
        }
        //计算公式
        int val = (int) (attack * getBet(owner, target));
        val = fixHp(owner, target, val, room, config);
        context.addValue(target, UnitValue.HP, -val);
        targetReport.addValue(ValueItem.valueOf(UnitValue.HP, -val));
        autoElimi(room, owner, skillReport, chessBoard);

    }

    @Override
    public EffectType getType() {
        return EffectType.SK_CONVERT;
    }
}
